//package com.ocom.gateway.config.limiter;
//
////import net.sunmonkey.db.model.PathRate;
////import net.sunmonkey.gateway.service.PathRateService;
//
//import com.ocom.gateway.config.limiter.MyRedisRateLimiter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * 接口限流过滤器
// */
//@Component
//public class PathRateGlobalFilter implements Ordered, GlobalFilter {
//    @Autowired
//    private MyRedisRateLimiter myRedisRateLimiter;
//
////    @Autowired
////    private PathRateService pathRateService;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        //获取到该接口路径        String path = exchange.getRequest().getPath().value();
//        //从数据库中获取到该接口对于的限流参数
//        PathRate pathRate = pathRateService.get(path);
//
//        //如果允许同行，没有超过该接口的流量限制
//        if(myRedisRateLimiter.isAllowed("path:"+path+":",
//                pathRate.getReplenishRate(),
//                pathRate.getBurstCapacity())){
//            return chain.filter(exchange);
//        }        //如果不允许此次请求通过，就返回429，请求太频繁的错误        else{
//        exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
//        return exchange.getResponse().setComplete();
//    }
//}
//
//    @Override
//    public int getOrder() {
//        return 1;
//    }
//}